package com.qikux.controller;

import cn.hutool.captcha.AbstractCaptcha;
import com.qikux.annotation.*;
import com.qikux.annotition.Authentication;
import com.qikux.consts.AppConst;
import com.qikux.entity.ResourceComment;
import com.qikux.entity.ResourceType;
import com.qikux.entity.User;
import com.qikux.service.ResourceService;
import com.qikux.utils.ContextUtils;
import com.qikux.utils.UploadUtils;
import com.qikux.vo.Page;
import com.qikux.vo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.util.List;
import java.util.Map;


@Component
@RequestMapping("/res")
public class ResourceController {

    @Resource
    private ResourceService resourceService ;

    @RequestMapping("/types")
    @ResponseBody
    public ResultVo<List<ResourceType>> types() {

        // 调用业务逻辑层，查询所有的资源类型
        return resourceService.queryTypes();
    }


    @Authentication
    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public String upload() {
        return "upload" ;
    }

    @Authentication
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo<String> uploadResource() throws Exception{
        // 获取上传的资源
        Part part = ContextUtils.getRequest().getPart("resource");

        // 将资源进行上传
        String path = UploadUtils.upload(part, "upload.res.dir") ;

        return ResultVo.ok().body(path) ;
    }

    @Authentication
    @RequestMapping(value = "/uploadRes", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo<String> uploadRes(HttpServletRequest request) {
        // 1. 获取资源
        String resource = request.getParameter("resource");
        // 2. 获取资源名
        String resourceName = request.getParameter("resourceName");
        // 3. 获取资源描述
        String resourceDesc = request.getParameter("resourceDesc");
        // 4. 获取关键字
        String keywords = request.getParameter("keywords");
        // 5. 获取上传的资源类型ID
        Long typeId = Long.parseLong(request.getParameter("typeId"));
        // 6. 获取积分
        int score = Integer.parseInt(request.getParameter("score"));
        // 7. 获取验证码
        String code = request.getParameter("code") ;
        // 8. 获取文件真实的后缀
        String ext = request.getParameter("ext") ;
        // 9. 获取文件大小
        long size = Long.parseLong(request.getParameter("size"));

        // 判断验证码是否正确
        AbstractCaptcha captcha = (AbstractCaptcha)ContextUtils.getSession().getAttribute(AppConst.CODE);

        if (captcha == null || !captcha.verify(code)) {
            return ResultVo.fail("验证码不正确").build() ;
        }

        ContextUtils.getSession().removeAttribute(AppConst.CODE);


        // 从 Session中获取当前登录的用户ID
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);

        // 组装数据
        com.qikux.entity.Resource res = com.qikux.entity.Resource.builder().resource(resource)
                .resourceName(resourceName)
                .resourceDesc(resourceDesc)
                .keywords(keywords)
                .typeId(typeId)
                .score(score)
                .ext(ext)
                .size(size)
                .userId(user.getId())
                .build();

        // 调用业务逻辑层 保存资源
        return resourceService.saveResource(res);

    }

    /**
     * 主页导航栏展示
     * @return
     */
    @RequestMapping(value="/reses",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<List<ResourceType>> reses(HttpServletRequest request){
        String pid = request.getParameter("id");
        return resourceService.queryReses(pid);
    }

    /**
     * 热门资源和导航选择
     */
    @RequestMapping(value = "/reshot",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Page<Map<String,Object>>> reshot(HttpServletRequest request) {
        //获取hot参数
        //String hot = request.getParameter("hot");
        //获取导航栏id
        String id = request.getParameter("id");

        //获取分页页码
        String page = request.getParameter("page");
        if(page==null||"".equals(page)) page = "1";
        int _page = Integer.parseInt(page);
        /**
         * 分页数查询
         */
        Page<Map<String,Object>> pageBean = Page.builder().page(_page).rows(12).build();
        if (id!="") {
            int _id = Integer.parseInt(id);
            return resourceService.queryRes(_id,pageBean);
        }
        return resourceService.queryHot(0,pageBean);
    }

    /**
     * @description 提交资源评论
     * @date 2022/9/28 9:23
     * @auther XuHuanDong
     */
    @Authentication
    @RequestMapping(value = "/reviews",method = RequestMethod.POST)
    @ResponseBody
    public ResultVo<String> resReviews(HttpServletRequest request){

        //获取输入的评论内容、评论星级、资源上传者
        String content = request.getParameter("content");
        String start = request.getParameter("start");
        String resId = request.getParameter("resId");
        String resUserId = request.getParameter("resUserId");
        // 从 Session中获取当前登录的用户ID
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        if (Long.parseLong(resUserId) == user.getId()){
            return ResultVo.fail("不可评论自己的资源").build();
        }
        //组装数据
        ResourceComment resourceComment = ResourceComment.builder().comContent(content)
                .stars(Integer.parseInt(start)).userId(user.getId())
                .resId(Long.parseLong(resId)).build();
        //调用业务层 保存评论
        return resourceService.saveResComment(resourceComment);
    }
    /**
     * 全局查询
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryF",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Page<Map<String,Object>>> queryF(HttpServletRequest request){

        //获取模糊查询字段
        String Fname = request.getParameter("name");
        //获取分页页码
        String page = request.getParameter("page");
        if(page==null||"".equals(page)) page = "1";
        int _page = Integer.parseInt(page);
        /**
         * 分页数查询
         */
        Page<Map<String,Object>> pageBean = Page.builder().page(_page).rows(12).build();
        return resourceService.queryFuzzy(Fname,pageBean);
    }

}
